/**
 * 按交易跳转集群设置
 */
angular.module('inspinia',['uiSwitch','infinity.angular-chosen']).controller("addQuotaCtrl", function($scope, $http, $state, $stateParams, i18nService,SweetAlert,$document) {
	$scope.paginationOptions=angular.copy($scope.paginationOptions);
	i18nService.setCurrentLang('zh-cn');

	$scope.quotas = [];
	$scope.query = function() {
		$http.post('policyTarget/selectQuotas', "&pageNo="+$scope.paginationOptions.pageNo+"&pageSize="+$scope.paginationOptions.pageSize,
       		 {headers: {'Content-Type': 'application/x-www-form-urlencoded'}}
        ).success(function(data){
			$scope.policyTargetGrid.data = data.page.result;
			$scope.policyTargetGrid.totalItems = data.page.totalCount;
			$scope.quotas = data.quotas;
        }).error(function(){
        }); 
	};
	$scope.query();

	$scope.addOne = function(){
		var quota = {"quotaAmount": '', "createTime":''};
		$scope.quotas.push(quota);
	}
	$scope.delrule = function (val,item) {
		/*if ( $scope.quotas.length===1){
			$scope.notice("最后一条设置不允许删除");
			return;
		}*/
		$scope.quotas.splice(val,1);
	}

	$scope.saveQuotas = function(){


		SweetAlert.swal({
				title: "我已经确认数据更新时间节点设置无误！",
				type: "warning",
				showCancelButton: true,
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				closeOnConfirm: true,
				closeOnCancel: true },
			function (isConfirm) {
				if (isConfirm) {
					$http.post("policyTarget/saveQuotas", $scope.quotas)
						.success(function(result){
							if(result.status){
								$scope.notice("操作成功");
								/*$state.transitionTo('org.policyTarget',null,{reload:true});*/
							} else {
								$scope.notice(result.msg);
							}
						}).error(function(){
						//$scope.notice("操作失败");
					});
				}
			});

	}


	$scope.policyTargetGrid = {
	        paginationPageSize:10,                  //分页数量
	        paginationPageSizes: [10,20,50,100],	//切换每页记录数
	        useExternalPagination: true,		  //开启拓展名
	        columnDefs: [
				{field: 'quotaAmount',displayName: '目标总额度（元）',width: 150,pinnable: false,sortable: false},
				{field: 'operator',displayName: '操作人',width: 150,pinnable: false,sortable: false},
				{field: 'createTime',displayName: '操作时间',width: 150,pinnable: false,sortable: false, cellFilter:'date:"yyyy-MM-dd HH:mm:ss"'},
				{field: 'effectiveTime',displayName: '生效时间',width: 150,pinnable: false,sortable: false, cellFilter:'date:"yyyy-MM-dd HH:mm:ss"'},
	        ],
	        onRegisterApi: function(gridApi) {                
	            gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
	            	$scope.paginationOptions.pageNo = newPage;
	            	$scope.paginationOptions.pageSize = pageSize;
	            	$scope.query();
	            });
	        }
	};

	//页面绑定回车事件
	$document.bind("keypress", function(event) {
		$scope.$apply(function (){
			if(event.keyCode == 13){
				$scope.query();
			}
		})
	});
	
});